<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    <div class="container-div">
    	<div class="col-sm-12 select-info">
			<form id="classList-form">
				<div class="select-list">
					<ul>
						<li>
							<label class="search-select2-label">宿舍号：</label>
							<div class="search-select2-container">
								<select id="dormitoryNumber" name="dormitoryNumber">
									<option value="">请选择</option>
								</select>
							</div>
						</li>
						<li>
							<label>日期：</label>
							<input type="text" class="time time-range" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
							<span>-</span>
							<input type="text" class="time time-range" id="endTime" placeholder="结束时间" name="params[endTime]"/>
						</li>
						<li>
							<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-success btn-rounded btn-sm" onclick="$.table.exportExcel()"><i class="fa fa-download"></i>&nbsp;下载</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="resetSearchFrom()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</form>
		</div>
		<div class="btn-group hidden-xs" id="toolbar" role="group">
			<a class="btn btn-outline btn-success btn-rounded" onclick="$.operate.add()" shiro:hasPermission="module-teacher:civilized-dormitory:add">
				<i class="fa fa-plus"></i> 新增
			</a>
			<a class="btn btn-outline btn-danger btn-rounded" onclick="$.operate.batRemove()" shiro:hasPermission="module-teacher:civilized-dormitory:remove">
				<i class="fa fa-trash-o"></i> 删除
			</a>
		</div>
			
		<div class="col-sm-12 select-info table-striped">
			<table id="bootstrap-table" data-mobile-responsive="true"></table>
		</div>
    </div>
    <div th:include="include :: footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('module-teacher:civilized-dormitory:edit')}]];
        var removeFlag = [[${@permission.hasPermi('module-teacher:civilized-dormitory:remove')}]];
        var prefix = ctx + "module/teacher/civilized-dormitory"
        
        function resetSearchFrom(){
     		$.form.reset();
     		$('#dormitoryNumber').val(null).trigger("change");
     	}
        
        $(function() {
        	//加载查询选项
        	$("#dormitoryNumber").select2({
				ajax: {
			        type:'GET',
			        url: prefix + "/queryDormFormatSelect2",
			        delay: 250,
			        cache: true,
			        processResults: function (data, params) {
			        	data.results.forEach(function(item){
			        		item.id = item.text;
			        	});
			        	return data;
				    }
			    }
			});
        	
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                modalName: "文明宿舍",
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : '序号' 
				},
				{
					field : 'dormitoryNumber', 
					title : '宿舍号' 
				},
				{
					field : 'masterTeacherNameArray',
					title : '班主任',
					formatter : function(value){
						return value.join(",");
					}
				},
				{
					field : 'nowClassNameArray', 
					title : '所属班级',
					formatter : function(value){
						return value.join(",");
					}
				},
				{
					field : 'menberNameArray', 
					title : '宿舍成员',
					formatter : function(value){
						return value.join(",");
					}
				},
				{
					field : 'belongDate', 
					title : '对应的日期' 
				},
				{
					field : 'remark', 
					title : '备注' 
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }],
	            responseHandler: function(result){
	            	//格式化服务端响应过来的数据
	            	result.rows.forEach(function(row){
	            		row.menberNameArray = [];
	            		row.nowClassNameArray = [];
	            		row.masterTeacherNameArray =[];
	            		row.dormitoryMenber.forEach(function(item){
	            			if(row.menberNameArray.indexOf(item.name) == -1){
	            				row.menberNameArray.push(item.name);
							}
	            			if(row.nowClassNameArray.indexOf(item.nowClassName) == -1){
	            				row.nowClassNameArray.push(item.nowClassName);
            				}
	            			if(row.masterTeacherNameArray.indexOf(item.masterTeacherName) == -1){
	            				row.masterTeacherNameArray.push(item.masterTeacherName);
	            			}
	            		})
	            	});
	            	return result;
	            }
            };
            $.table.init(options);
        });
    </script>
</body>
</html>